//
//  BaseViewController.m
//  SHOP
//
//  Created by caiyc on 16/11/25.
//  Copyright © 2016年 changce. All rights reserved.
//

#import "BaseViewController.h"
#import "UIImage+GIF.h"
#import "BZ_ComenLoadAnimation.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width

@interface BaseViewController ()
@property(nonatomic,strong)UIImageView *noDataImg;
@property(nonatomic,strong)UILabel *noDataStr;
@end

@implementation BaseViewController
-(UIImageView *)noDataImg{
    if(!_noDataImg){
        _noDataImg = [[UIImageView alloc]initWithFrame:CGRectMake(67, screen_height/2-84, 240, 128)];
        _noDataImg.image = [UIImage imageNamed:@"暂无数据 (1)"];
        _noDataImg.contentMode = UIViewContentModeScaleAspectFit;
        [self.view addSubview:_noDataImg];
    }
    return _noDataImg;
}
-(UILabel *)noDataStr{
    if(!_noDataStr){
        _noDataStr = [[UILabel alloc]initWithFrame:CGRectMake(76, screen_height/2+64, 224, 21)];
        _noDataStr .textAlignment = NSTextAlignmentCenter;
        _noDataStr.font = [UIFont systemFontOfSize:14];
        _noDataStr.textColor = [UIColor darkGrayColor];
        [self.view addSubview:_noDataStr];
    }
    return _noDataStr;
}
-(void)showNoDataView:(NSString *)str{
    [self noDataImg];
    [self noDataStr];
    self.noDataStr.text = str;
}
-(void)hideNoDataView{
    [self.noDataStr removeFromSuperview];
    [self.noDataImg removeFromSuperview];
}
-(void)showLoading{
    UIImageView *imageView = [[UIImageView alloc]init];
    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.mode = MBProgressHUDModeCustomView;
    HUD.label.text = @"加载中。。。";
    HUD.customView = imageView;
    [HUD showAnimated:1];
}
//get请求
-(void)getRequestWithString:(NSString *)urlStr andParams:(NSDictionary *)params andAcceptableContentTypes:(NSSet *)acceptableContentTypes
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes=acceptableContentTypes;
    [manager GET:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
-(void)getRequestWithString:(NSString *)urlStr andParams:(NSDictionary *)params
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    //manager.responseSerializer.acceptableContentTypes=acceptableContentTypes;
    [manager GET:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       
    }];
}
- (void)getWithURLString:(NSString *)URLString
              parameters:(id)parameters
                 success:(void (^)(id response))success
                 failure:(void (^)(NSError * error))failure {
    
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", nil]];
    /**
     *  可以接受的类型
     */
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    /**
     *  请求队列的最大并发数
     */
    //    manager.operationQueue.maxConcurrentOperationCount = 5;
    /**
     *  请求超时的时间
     */
    //    manager.requestSerializer.timeoutInterval = 5;
    NSString *urlString = [NSString stringWithFormat:@"%@%@",BASE_URL,URLString];
    [manager GET:urlString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
      
        if (success) {
            if([[responseObject objectForKey:@"status"]integerValue]==1)
            {
                responseObject = [NSDictionary changeType:responseObject];
                success(responseObject);
            }
            else
            {
                [WToast showWithText:responseObject[@"msg"]];
                success(nil);
            }
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
           
        }
    }];
}
- (void)getWithparameters:(id)parameters
                 success:(void (^)(id response))success
                 failure:(void (^)(NSError * error))failure {
    
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
     [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", nil]];
    /**
     *  可以接受的类型
     */
    //manager.requestSerializer=[AFJSONRequestSerializer serializer];
    /**
     *  请求队列的最大并发数
     */
    //    manager.operationQueue.maxConcurrentOperationCount = 5;
    /**
     *  请求超时的时间
     */
    //    manager.requestSerializer.timeoutInterval = 5;
   // NSString *urlString = [NSString stringWithFormat:@"%@%@",BASE_URL,URLString];
    [manager GET:BASE_URL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            if([[responseObject objectForKey:@"status"]integerValue]==1)
            {
            success(responseObject);
            }
            else
            {
                success(nil);
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}
- (void)postWithURLString:(NSString *)URLString
               parameters:(id)parameters
                  success:(void (^)(id response))success
                  failure:(void (^)(NSError * error))failure {
    
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", nil]];
    NSString *urlString = [NSString stringWithFormat:@"%@%@",BASE_URL,URLString];
    NSLog(@"...url...%@",urlString);
    [manager POST:urlString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@".....response...%@",responseObject);
        if (success) {
            if([[responseObject objectForKey:@"status"]integerValue]==1)
            {
                success(responseObject);
            }
            else
            {
                [WToast showWithText:[responseObject objectForKey:@"msg"]];
                success(nil);
            }

        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败:%@",error);
        if (failure) {
            failure(error);
        }
    }];
    
}
//弹幕拉取方法
-(void)loadDm:(id)param success:(void (^)(id response))success
             failure:(void (^)(NSError * error))failure{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:param];
    [params setObject:@"10" forKey:@"i"];
    [params setObject:@"bz_activities" forKey:@"m"];
    [params setObject:@"entry" forKey:@"c"];
    
    RoleType type = [[XTool GetDefaultInfo:Prole] integerValue];
    switch (type) {
        case RoleType_YH:{//普通用户
            if([XTool GetDefaultInfo:USER_INFO]){
                NSDictionary *user_Info = [XTool GetDefaultInfo:USER_INFO];
                [params setObject:user_Info[@"token"] forKey:@"token"];
            }
            break;
        }
        case RoleType_ZB:{//主播
            if([XTool GetDefaultInfo:HOST_USER]){
                NSDictionary *user_Info = [XTool GetDefaultInfo:HOST_USER];
                [params setObject:user_Info[@"token"] forKey:@"token"];
            }
            break;
        }
        case RoleType_SJ:{//商户
            if([XTool GetDefaultInfo:BUSSION_INFO]){
                NSDictionary *user_Info = [XTool GetDefaultInfo:BUSSION_INFO];
                [params setObject:user_Info[@"token"] forKey:@"token"];
            }
            break;
        }
        case RoleType_GHS:{//供货商
            break;
        }
    }
    
    [manager POST:BASE_URL parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success) {
            if([[responseObject[@"message"] objectForKey:@"resultCode"]integerValue]==0||[[responseObject[@"message"] objectForKey:@"resultCode"]integerValue]==1)
            {
                success(responseObject[@"message"]);
            }
            else
            {
           success(nil);
            }
            
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];
}
//带进度条的请求
-(void)weqPostHandle:(id)param success:(void (^)(id response))success
             failure:(void (^)(NSError * error))failure haveProgress:(BOOL)progress{
//    MBProgressHUD*HUD = [[MBProgressHUD alloc] initWithView:self.view];
//        [self.view addSubview:HUD];
//        HUD.label.text = @"人脸数据认证中";
//        //细节文字
//        HUD.detailsLabel.text = @"请耐心等待";
//        //显示对话框
//        [HUD showAnimated:YES];
   
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:param];
    [params setObject:@"10" forKey:@"i"];
    [params setObject:@"bz_activities" forKey:@"m"];
    [params setObject:@"entry" forKey:@"c"];
    
    RoleType type = [[XTool GetDefaultInfo:Prole] integerValue];
    switch (type) {
        case RoleType_YH:{//普通用户
            if([XTool GetDefaultInfo:USER_INFO]){
                NSDictionary *user_Info = [XTool GetDefaultInfo:USER_INFO];
                [params setObject:user_Info[@"token"] forKey:@"token"];
            }
            break;
        }
        case RoleType_ZB:{//主播
            if([XTool GetDefaultInfo:HOST_USER]){
                NSDictionary *user_Info = [XTool GetDefaultInfo:HOST_USER];
                [params setObject:user_Info[@"token"] forKey:@"token"];
            }
            break;
        }
        case RoleType_SJ:{//商户
            if([XTool GetDefaultInfo:BUSSION_INFO]){
                NSDictionary *user_Info = [XTool GetDefaultInfo:BUSSION_INFO];
                [params setObject:user_Info[@"token"] forKey:@"token"];
            }
            break;
        }
        case RoleType_GHS:{//供货商
            break;
        }
    }

    [manager POST:BASE_URL parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
       
       
        if (success) {
            if([[responseObject[@"message"] objectForKey:@"resultCode"]integerValue]==0||[[responseObject[@"message"] objectForKey:@"resultCode"]integerValue]==1)
            {
                success(responseObject[@"message"]);
            }
            else
            {
                [WToast showWithText:[responseObject[@"message"] objectForKey:@"resultMessage"]];
                
                success(nil);
            }
            
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        
    }];
}
-(void)weqPostHandle:(id)param success:(void (^)(id response))success
             failure:(void (^)(NSError * error))failure hasProgress:(BOOL)progress{
    BZ_ComenLoadAnimation *animation;
    if(progress==1){
        animation  = [[BZ_ComenLoadAnimation alloc]init];
        [animation showLoadingView];
        [[[UIApplication sharedApplication]keyWindow]addSubview:animation];
    }
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:param];
    [params setObject:@"10" forKey:@"i"];
    [params setObject:@"bz_activities" forKey:@"m"];
    [params setObject:@"entry" forKey:@"c"];
    
    RoleType type = [[XTool GetDefaultInfo:Prole] integerValue];
    switch (type) {
        case RoleType_YH:{//普通用户
            if([XTool GetDefaultInfo:USER_INFO]){
                NSDictionary *user_Info = [XTool GetDefaultInfo:USER_INFO];
                [params setObject:user_Info[@"token"] forKey:@"token"];
            }
            break;
        }
        case RoleType_ZB:{//主播
            if([XTool GetDefaultInfo:HOST_USER]){
                NSDictionary *user_Info = [XTool GetDefaultInfo:HOST_USER];
                [params setObject:user_Info[@"token"] forKey:@"token"];
            }
            break;
        }
        case RoleType_SJ:{//商户
            if([XTool GetDefaultInfo:BUSSION_INFO]){
                NSDictionary *user_Info = [XTool GetDefaultInfo:BUSSION_INFO];
                [params setObject:user_Info[@"token"] forKey:@"token"];
            }
            break;
        }
        case RoleType_GHS:{//供货商
            break;
        }
    }
    
    
    [manager POST:BASE_URL parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if(progress){
            [animation removeView];
        }
        
        
        if (success) {
            if([[responseObject[@"message"] objectForKey:@"resultCode"]integerValue]==0||[[responseObject[@"message"] objectForKey:@"resultCode"]integerValue]==1)
            {
                success(responseObject[@"message"]);
            }
            else
            {
                [WToast showWithText:[responseObject[@"message"] objectForKey:@"resultMessage"]];
                success(nil);
            }
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if(progress){
            [animation removeView];
        }
    }];
}
//微擎请求的封装方法
-(void)weqPostHandle:(id)param success:(void (^)(id response))success
             failure:(void (^)(NSError * error))failure{

    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
      manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:param];
    [params setObject:@"10" forKey:@"i"];
    [params setObject:@"bz_activities" forKey:@"m"];
    [params setObject:@"entry" forKey:@"c"];
    
    RoleType type = [[XTool GetDefaultInfo:Prole] integerValue];
    switch (type) {
        case RoleType_YH:{//普通用户
            if([XTool GetDefaultInfo:USER_INFO]){
                NSDictionary *user_Info = [XTool GetDefaultInfo:USER_INFO];
                [params setObject:user_Info[@"token"] forKey:@"token"];
            }
            break;
        }
        case RoleType_ZB:{//主播
            if([XTool GetDefaultInfo:HOST_USER]){
                NSDictionary *user_Info = [XTool GetDefaultInfo:HOST_USER];
                [params setObject:user_Info[@"token"] forKey:@"token"];
            }
            break;
        }
        case RoleType_SJ:{//商户
            if([XTool GetDefaultInfo:BUSSION_INFO]){
                NSDictionary *user_Info = [XTool GetDefaultInfo:BUSSION_INFO];
                [params setObject:user_Info[@"token"] forKey:@"token"];
            }
            break;
        }
        case RoleType_GHS:{//供货商
            break;
        }
    }
    
    
    [manager POST:BASE_URL parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
       // SLog(@"请求结果：%@",responseObject);
       
        if (success) {
            if([[responseObject[@"message"] objectForKey:@"resultCode"]integerValue]==0||[[responseObject[@"message"] objectForKey:@"resultCode"]integerValue]==1)
            {
                success(responseObject[@"message"]);
            }
            else
            {
                [WToast showWithText:[responseObject[@"message"] objectForKey:@"resultMessage"]];
                success(nil);
            }
            }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
    }];
}
- (void)postWithparameters:(id)parameters
                  success:(void (^)(id response))success
                  failure:(void (^)(NSError * error))failure {

    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/json", nil]];
    [manager POST:BASE_URL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            if([[responseObject objectForKey:@"status"]integerValue]==1)
            {
                success(responseObject);
            }
            else
            {
                success(nil);
            }
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败:%@",error);
        if (failure) {
            failure(error);
        }
    }];
    
}

//post请求
-(void)postRequestWithString:(NSString *)urlStr andParams:(NSDictionary *)params andAcceptableContentTypes:(NSSet *)acceptableContentTypes
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes=acceptableContentTypes;
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    [manager POST:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败:%@",error);
    }];
}
-(void)postRequestWithString:(NSString *)urlStr andParams:(NSDictionary *)params
{
    
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    //manager.responseSerializer.acceptableContentTypes=acceptableContentTypes;
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    [manager POST:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败:%@",error);
    }];
}

- (void)initNavgationBarWithTitle:(NSString *)titleStr
{
    UILabel *labelNavigation = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, ScreenWidth - 200, 64)];
    labelNavigation.text = titleStr;
    //  labelNavigation.textColor = [UIColor_ValueString colorWithHexString:NAGA_TITLE_COLOR];
    labelNavigation.font = [UIFont systemFontOfSize:17];
    labelNavigation.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = labelNavigation;
    [self setNavgationBackGroundImg];
}
/**
 *  设置导航条背景图片
 */
-(void)setNavgationBackGroundImgWithImgName:(NSString *)imgName
{
    UIImage *nav_bg = [UIImage imageNamed:imgName];
    [self.navigationController.navigationBar setBackgroundImage:nav_bg forBarMetrics:UIBarMetricsDefault];
}
-(void)setNavgationBackGroundImgWithImg:(UIImage *)img{
    [self.navigationController.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
}
-(void)setNavgationBackGroundImg{
    //[self setNavgationBackGroundImgWithImg:[self imageWithColor:[UIColor_ValueString colorWithHexString:NAGA_BACKGROUND_COLOR]]];
}
-(void)setNavgationBack
{
    UIButton *btn_back = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn_back setImage:[UIImage imageNamed:@"navigation_back_normal"] forState:UIControlStateNormal];
    //    [btn_back setImage:[UIImage imageNamed:@"navigation_back_highlight"] forState:UIControlStateHighlighted];
    [btn_back addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    btn_back.frame = CGRectMake(0, 0, 23 , 40);
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn_back];
}
/**
 *  设置导航烂右边
 */
-(void)setUpRightButtonItem:(NSInteger)type style:(BZButtonImageStyle)style Image:(NSString *)imageStr title:(NSString *)title target:(SEL)target width:(NSInteger)width titleColor:(UIColor*)titleColor titleFont:(NSInteger)titleFont{
    /**
     *  type 1为普通文字 2为带图和文字
     */
    if(type==1){
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, width, 40);
        if(imageStr){
        [btn setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];
        }
        if(title){
         [btn setTitle:title forState:UIControlStateNormal];
        }
         btn.titleLabel.font = [UIFont systemFontOfSize:titleFont];
        [btn setTitleColor:titleColor forState:UIControlStateNormal];
        [btn addTarget:self action:target forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem =  [[UIBarButtonItem alloc] initWithCustomView:btn];;
    }else{
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, width, 40);
        btn.titleLabel.font = [UIFont systemFontOfSize:titleFont];
         [btn setTitleColor:titleColor forState:UIControlStateNormal];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];
        [btn layoutButtonWithImageStyle:style imageTitleToSpace:10];
        [btn addTarget:self action:target forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem =  [[UIBarButtonItem alloc] initWithCustomView:btn];;
    }
}
-(UIButton *)getNavgationBackBtn
{
    UIButton *btn_back = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn_back setImage:[UIImage imageNamed:@"navigation_back_normal"] forState:UIControlStateNormal];
    //    [btn_back setImage:[UIImage imageNamed:@"navigation_back_highlight"] forState:UIControlStateHighlighted];
    [btn_back addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    btn_back.frame = CGRectMake(0, 0, 23 , 40);
    
    return btn_back;
}
-(void)backRootController{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)backBtnAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)setNavgationSearchBarWithPlaceholder:(NSString *)placeholder tag:(NSInteger)tag target:(id)target{
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.frame = CGRectMake(50.0f, 14.0f,self.navigationController.navigationBar.frame.size.width - 100.f , 20.0f);//
    searchBar.placeholder = placeholder;
    searchBar.delegate = target;
    searchBar.tag = 300;
    [self.navigationController.navigationBar addSubview:searchBar];
}

-(void)removeNavgationSearchBar{
    [[self.navigationController.navigationBar viewWithTag:300] removeFromSuperview];
}

-(void)setNavgationLeftWithName:(NSString *)name font:(UIFont *)font color:(UIColor *)color size:(CGSize)size action:(SEL)action
{
    UIButton *btn_left = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn_left addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    btn_left.frame = CGRectMake(0, 0, 50 , 44);
    btn_left.titleLabel.font = font;
    [btn_left setTitleColor:color forState:UIControlStateNormal];
    [btn_left setTitle:name forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn_left];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = YES;
    //self.automaticallyAdjustsScrollViewInsets=YES;
    // 设置CGRectZero从导航栏下开始计算
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout =UIRectEdgeNone;
    }
   // [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(addShopNum:) name:@"addShopNum" object:nil];


}


-(void)addShopNum:(NSNotification *)no
{

}
-(void)deleteShopCar:(NSNotification *)no
{
    [self performSelectorInBackground:@selector(reloadShopChar) withObject:nil];
}

-(void)reloadShopChar
{
    NSDictionary *userDic = [XTool GetDefaultInfo:USER_INFO];
    NSDictionary *dic = @{@"user_id":userDic[USER_ID]};
    [self postWithURLString:@"/Cart/cartList" parameters:dic success:^(id response) {
        NSLog(@"获取数据:---------%@",response);
        if(response)
        {
            [XTool SaveDefaultInfo:response Key:SHOPCARDATA];
            NSInteger num = [[response objectForKey:@"result"][@"cartList"]count];
            self.notiLb.text = [NSString stringWithFormat:@"%ld",(long)num];
            self.notiLb.backgroundColor = [UIColor redColor];
            NSString *nums =  [NSString stringWithFormat:@"%@",[response objectForKey:@"result"][@"total_price"][@"num"]];
            NSDictionary *userDic = @{@"num":nums};
            [[NSNotificationCenter defaultCenter]postNotificationName:@"addshopNumber" object:userDic];

        }
    } failure:^(NSError *error) {
        
    }];

}
//-(void)initNaviView:(UIColor *)color title:(NSString *)title haveLeft:(BOOL)left right:(NSString *)right  rightAction:(SEL)rightAction
//{
//     self.naviView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screen_width, naviHei)];
//     self.naviView.backgroundColor = RGB(225, 29, 66);
//   // self.naviView.backgroundColor = color;
//    [self.view addSubview:self.naviView];
//    
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((screen_width-200)/2, 20, 200, naviHei-20)];
//    label.text = title;
//    label.textAlignment = 1;
//    label.textColor = [UIColor whiteColor];
//    label.font = [UIFont systemFontOfSize:18];
//    [self.naviView addSubview:label];
//    
//    if(left)
//    {
//    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [leftBtn setFrame:CGRectMake(leftSpece,(naviHei-btnSize)/2+10 , btnSize, btnSize)];
//      //  [leftBtn setImage:[UIImage imageNamed:@"backs"] forState:UIControlStateNormal];
//        [leftBtn ButtonWithIconStr:@"\U0000e7c8" inIcon:iconfont andSize:CGSizeMake(btnSize, btnSize) andColor:[UIColor whiteColor] andiconSize:PAPULARFONTSIZE];
//
//    //[leftBtn ButtonWithIconStr:@"\U0000e683" inIcon:iconfont andSize:CGSizeMake(btnSize, btnSize) andColor:[UIColor whiteColor] andiconSize:PAPULARFONTSIZE];
//    [leftBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
//    [self.naviView addSubview:leftBtn];
//    }
//    if(![right isEqualToString:@""])
//    {
//        NSLog(@"right==%@",right);
////        NSMutableString *str =@"\"U0000e";
////     str =   [str stringByAppendingString:right];
//        //NSString *str = @"\U0000e682";
//       // NSString *str = [NSString stringWithFormat:@"\U0000e%@",right];
//    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [rightBtn setFrame:CGRectMake(screen_width-leftSpece-btnSize,(naviHei-btnSize)/2+10 , btnSize, btnSize)];
//    [rightBtn ButtonWithIconStr:right inIcon:iconfont andSize:CGSizeMake(btnSize, btnSize) andColor:[UIColor whiteColor] andiconSize:PAPULARFONTSIZE];
//    [rightBtn addTarget:self action:rightAction forControlEvents:UIControlEventTouchUpInside];
//   // [rightBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
//    [self.naviView addSubview:rightBtn];
//    }
//
////    UILabel *speLine = [[UILabel alloc]init];
////    speLine.backgroundColor = separaterColor;
////    speLine.frame = CGRectMake(0, naviHei-1, screen_width, 1);
////    [self.naviView addSubview:speLine];
//  
//    
//}
-(void)setNavigation:(BOOL)haslft andTitle:(NSString *)title andNaviColor:(UIColor *)cloor andTitleColor:(UIColor *)titlecolor{
    NSInteger height = KISIphoneX?88:naviHei;
    self.naviView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screen_width, height)];
    if(cloor)
    {
        self.naviView.backgroundColor = cloor;
    }else
    self.naviView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.naviView];
    if(haslft){
        UIImageView *images = [[UIImageView alloc]init];
        [images setFrame:CGRectMake(leftSpece,(height-20)/2+10 , 20, 20)];
        images.image = [UIImage imageNamed:@"左551"];
        images.contentMode = UIViewContentModeScaleAspectFit;
        [self.naviView addSubview:images];
        UILabel *label = [[UILabel alloc]init];
        label.frame = CGRectMake(leftSpece+30,(height-20)/2+10 , 200, 20);
        label.text = title;
        label.font = [UIFont systemFontOfSize:18];
        label.textColor = titlecolor;
        [self.naviView addSubview:label];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(leftSpece,(height-50)/2+10 , 200, 50);
       [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [self.naviView addSubview:btn];
    }
}
-(void)setNavigation:(BOOL)haslft andTitle:(NSString *)title andNaviColor:(UIColor *)cloor andTitleColor:(UIColor *)titlecolor andRightimg:(NSString *)imagepath andsel:(SEL)rightAction{
    NSInteger height = KISIphoneX?88:naviHei;
    self.naviView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screen_width, height)];
    if(cloor){
        self.naviView.backgroundColor = cloor;
    }else{
        self.naviView.backgroundColor = [UIColor whiteColor];
    }
    [self.view addSubview:self.naviView];
    
    if(haslft){
        UIImageView *images = [[UIImageView alloc]init];
        [images setFrame:CGRectMake(leftSpece,(height-20)/2+10 , 20, 20)];
        images.image = [UIImage imageNamed:@"左551"];
        images.contentMode = UIViewContentModeScaleAspectFit;
        [self.naviView addSubview:images];
        UILabel *label = [[UILabel alloc]init];
        label.frame = CGRectMake(leftSpece+30,(height-20)/2+10 , 200, 20);
        label.text = title;
        label.font = [UIFont systemFontOfSize:18];
        label.textColor = titlecolor;
        [self.naviView addSubview:label];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(leftSpece,(height-50)/2+10 , 200, 50);
        [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [self.naviView addSubview:btn];
    }
    if(![imagepath isEqualToString:@""]){
        UIImageView *images = [[UIImageView alloc]init];
        [images setFrame:CGRectMake(screen_width-leftSpece-25,(height-20)/2+10 , 20, 20)];
        images.image = [UIImage imageNamed:imagepath];
        images.contentMode = UIViewContentModeScaleAspectFit;
        [self.naviView addSubview:images];
        
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setFrame:CGRectMake(screen_width-leftSpece-btnSize,(height-20)/2+10 , btnSize, btnSize)];
        // [rightBtn setImage:[UIImage imageNamed:imagepath] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:rightAction forControlEvents:UIControlEventTouchUpInside];
        [self.naviView addSubview:rightBtn];
    }
}
-(void)setB_Navigation:(BOOL)haslft andTitle:(NSString *)title andNaviColor:(UIColor *)cloor andTitleColor:(UIColor *)titlecolor andRightimg:(NSString *)imagepath andsel:(SEL)rightAction{
    NSInteger height = KISIphoneX?88:naviHei;
    self.naviView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screen_width, height)];
    if(cloor){
        self.naviView.backgroundColor = cloor;
    }else{
        self.naviView.backgroundColor = [UIColor whiteColor];
    }
    [self.view addSubview:self.naviView];
    
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(0,(height-20)/2+10 , screen_width, 20);
    label.text = title;
    label.textAlignment = NSTextAlignmentCenter;
    
    label.font = [UIFont systemFontOfSize:18];
    label.textColor = titlecolor;
    [self.naviView addSubview:label];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(leftSpece,(height-50)/2+10 , 200, 50);
    
    [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.naviView addSubview:btn];
    
    
    if(haslft){
        UIImageView *images = [[UIImageView alloc]init];
        [images setFrame:CGRectMake(leftSpece,(height-20)/2+10 , 20, 20)];
        images.image = [UIImage imageNamed:@"左551"];
        images.contentMode = UIViewContentModeScaleAspectFit;
        [self.naviView addSubview:images];
        }
    if(![imagepath isEqualToString:@""]){
        
        UIImageView *images = [[UIImageView alloc]init];
        [images setFrame:CGRectMake(screen_width-leftSpece-25,(height-20)/2+10 , 20, 20)];
        images.image = [UIImage imageNamed:imagepath];
        images.contentMode = UIViewContentModeScaleAspectFit;
        [self.naviView addSubview:images];
        
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setFrame:CGRectMake(screen_width-leftSpece-btnSize,(height-20)/2+10 , btnSize, btnSize)];
        [rightBtn addTarget:self action:rightAction forControlEvents:UIControlEventTouchUpInside];
        [self.naviView addSubview:rightBtn];
    }
}

-(void)initNaviView:(UIColor *)navColor hasLeft:(BOOL)haveLeft leftColor:(UIColor *)leftColor title:(NSString *)title titleColor:(UIColor *)titleColor right:(NSString *)right rightColor:(UIColor *)rightColor rightAction:(SEL)rightAction
{
    self.naviView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screen_width, naviHei)];
    //self.naviView.backgroundColor = navColor;
    self.naviView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.naviView];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((screen_width-200)/2, 20, 200, naviHei-20)];
    label.text = title;
    label.textAlignment = 1;
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:18];
    [self.naviView addSubview:label];
    
    if(haveLeft)
    {
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftBtn setFrame:CGRectMake(leftSpece,(naviHei-btnSize)/2+10 , btnSize, btnSize)];
        [leftBtn ButtonWithIconStr:@"\U0000e642" inIcon:iconfont andSize:CGSizeMake(btnSize, btnSize) andColor:[UIColor blackColor] andiconSize:PAPULARFONTSIZE];
        [leftBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [self.naviView addSubview:leftBtn];
    }
    if(![right isEqualToString:@""])
    {
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setFrame:CGRectMake(screen_width-leftSpece-btnSize,(naviHei-btnSize)/2+10 , btnSize, btnSize)];
        [rightBtn ButtonWithIconStr:right inIcon:iconfont andSize:CGSizeMake(btnSize, btnSize) andColor:[UIColor whiteColor] andiconSize:PAPULARFONTSIZE+5];
        [rightBtn addTarget:self action:rightAction forControlEvents:UIControlEventTouchUpInside];
        [self.naviView addSubview:rightBtn];
    }

}

-(void)setNotifationBage
{
    //每次进来都调用购物车数据
   self.notiLb = [[UILabel alloc]initWithFrame:CGRectMake(screen_width-leftSpece-15, (naviHei-btnSize)/2+12, 15, 15)];
    [self.naviView addSubview:self.notiLb];
    //
    self.notiLb.layer.cornerRadius =7.5;
    self.notiLb.layer.masksToBounds = 1;
    self.notiLb.textColor = [UIColor whiteColor];
    self.notiLb.font = [UIFont systemFontOfSize:11];
    self.notiLb.textAlignment = 1;

    
}
-(void)setNotifationBageNum:(NSInteger)num
{
 //   self.notiLb.text = @"5";
    if(num>0)
    {
    self.notiLb.backgroundColor = [UIColor redColor];
    self.notiLb.text = [NSString stringWithFormat:@"%ld",(long)num];
    }
}

-(void)back{
    [self.navigationController popViewControllerAnimated:NO];
}
-(void)pushView:(UIViewController *)viewController
{
    
    self.hidesBottomBarWhenPushed = 1;
    [self.navigationController pushViewController:viewController animated:NO];
   // self.hidesBottomBarWhenPushed = 0;
}
-(void)pushSecondVC:(UIViewController *)VC
{
    self.hidesBottomBarWhenPushed = 1;
    [self.navigationController pushViewController:VC animated:NO];
    self.hidesBottomBarWhenPushed = 0;
}
//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
